package com.java.forum.d_controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.forum.a_entity.Message;
import com.java.forum.a_entity.User;
import com.java.forum.c_service.MessageService;
import com.java.forum.e_util.JsonResult;
import com.java.forum.h_annotation.CurrentUser;
import com.java.forum.h_annotation.NeedToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("message")
public class MessageController extends BaseController {
    private MessageService messageService;

    @Autowired
    public void setMessageService(MessageService messageService) {
        this.messageService = messageService;
    }

    @NeedToken
    @RequestMapping("getHistory")
    public JsonResult<List<Message>> getHistoryMessage(Integer uid, long date, @CurrentUser User user) {
        Date d = new Date(date);
        PageHelper.startPage(1, 6);
        List<Message> messages = messageService.getHistoryMessage(d, user.getUid(), uid);
        PageInfo<Message> pageInfo = new PageInfo<>(messages);
        JsonResult<List<Message>> jsonResult = new JsonResult<>();
        jsonResult.setState(ok);
        jsonResult.setMessage(pageInfo.isHasNextPage() + "");
        jsonResult.setData(messages);
        return jsonResult;
    }
}
